#ifndef __I386_UTIL_IO_H__
#define __I386_UTIL_IO_H__

#include "common/basictypes.h"
#include "common/code16gcc.h"

/* Basic port I/O */
static inline void outb(u8 v, u16 port) {
  asm volatile(
      "outb %0,%1"
      :
      : "a" (v), "dN" (port)
               );
}

static inline u8 inb(u16 port) {
  u8 v;
  asm volatile(
      "inb %1,%0"
      : "=a" (v)
      : "dN" (port)
               );
  return v;
}

static inline void outw(u16 v, u16 port) {
  asm volatile("outw %0,%1"
               :
               : "a" (v), "dN" (port)
               );
}

static inline u16 inw(u16 port) {
  u16 v;
  asm volatile(
      "inw %1,%0"
      : "=a" (v)
      : "dN" (port)
               );
  return v;
}

static inline void outl(u32 v, u16 port) {
  asm volatile(
      "outl %0,%1"
      :
      : "a" (v), "dN" (port)
               );
}

static inline u32 inl(u32 port) {
  u32 v;
  asm volatile(
      "inl %1,%0"
      : "=a" (v)
      : "dN" (port)
               );
  return v;
}

static inline void io_delay(void) {
  const u16 DELAY_PORT = 0x80;
  asm volatile(
      "outb %%al,%0"
      :
      : "dN" (DELAY_PORT)
               );
}

#endif  // __I386_UTIL_IO_H__
